import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
import { Observable } from 'rxjs';

@Injectable()
export class AuthGuard implements CanActivate {
  canActivate(
    context: ExecutionContext,
  ): boolean | Promise<boolean> | Observable<boolean> {
    console.log('守卫执行了');

    // 如果直接去登录路由直接放行
    const req = context.switchToHttp().getRequest();
    if (req.path === '/admin/login') {
      return true;
    }

    const userInfo = req.session.username;
    if (userInfo) {
      return true;
    }

    return false;
  }
}
